<?php

namespace App\Http\Controllers\Business;

use App\Http\Controllers\Controller;
use App\Models\Business\PriceSetting;
use App\Common\Traits\Model\ModelSearchTrait;
use Illuminate\Http\Request;

class PriceSettingController extends Controller
{
    use ModelSearchTrait;

    //
    public function index(Request $request,PriceSetting $priceSetting)
    {
        $priceSetting=$this->singleModelSearch($request,$priceSetting,$this->priceSetCondition());
        $sets=$priceSetting->with(['serviceCategory'])->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        return $this->success($sets);
    }

    //
    public function priceSetCondition()
    {
        return [
            ['service_category_id','='],
        ];
    }

    //
    public function store(Request $request)
    {
        if(empty($request->service_category_id)){
            return $this->failed('服务项目id不能为空',201);
        }
        $data=$this->priceData($request);
        PriceSetting::create($data);
        return $this->success(['msg'=>'创建成功']);
    }
    //
    public function priceData($request)
    {
        return [
            'service_category_id'=>$request->service_category_id,
            'max_quantity'=>$request->max_quantity,
            'price'=>$request->price,
            'unit_price'=>$request->unit_price,
        ];
    }

    //
    public function show(Request $request)
    {
        $set=PriceSetting::with(['serviceCategory'])->find($request->price_set_id??0);
        return $this->success($set);
    }

    //
    public function update(Request $request, PriceSetting $priceSetting)
    {
        $set=$priceSetting->find($request->price_set_id??0);
        if(empty($set)){
            return $this->failed('id错误，无此价格设置',201);
        }
        if(empty($request->service_category_id)){
            return $this->failed('服务项目id不能为空',201);
        }
        $data=$this->priceData($request);
        $set->update($data);
        return $this->success(['msg'=>'编辑成功']);
    }

    //
    public function destroy(Request $request, PriceSetting $priceSetting)
    {
        $set=$priceSetting->find($request->price_set_id??0);
        if(empty($set)){
            return $this->failed('id错误，无此价格设置',201);
        }
        $set->delete();
        return $this->success(['msg'=>'删除成功']);
    }

    //
    public function getAllPriceSets(){
        $sets=PriceSetting::with(['serviceCategory'])->get();
        return $this->success($sets);
    }
}
